home *** CD-ROM | disk | FTP | other *** search
/ Aminet 48 / Aminet 48 (2002)(GTI - Schatztruhe)[!][Apr 2002].iso / Aminet / docs / mags / AIOV55.lha / AIOIssue55 / examples / 2.6_string.c < prev   
Encoding:
C/C++ Source or Header  |  1980-01-01  |  1.3 KB  |  28 lines

  1. #include <stdio.h>
  2.  
  3. int main(void)
  4. {
  5.   int name_len = 0;           /* Holds the length of the name */
  6.   int i = 0;
  7.   char pString[100];          /* Create a "string"; an array of 100 char */
  8.   char *pAlias = pString;     /* Create an alias for pString */
  9.  
  10.   memset(pString, 0, 100); /* init the first 100 chars of the string to all zeroes */
  11.   printf("\nEnter your first name (max 100 chars): ");
  12.   scanf("%s", pString); /* scanf will only accept one word; any characters after the first space, tab etc will be ignored */
  13.   while ( (*pAlias != 0) && (name_len < 100) ) /* Loop is designed to step through the string until the end of the name */
  14.   {                                            /* (char @ pAlias == NULL), or the string, is reached */
  15.     name_len++;
  16.     pAlias++;
  17.   }
  18.   /* Right now, pAlias is pointing just after the last character in the string. */
  19.   printf("\nName is %i characters long. Name spelt backwards: ", name_len);
  20.   while (pAlias >= pString) /* Loop until pAlias points to pString again */
  21.   {     /* The job for this loop is to step through the string backwards, printing each character as it goes. */
  22.     printf("%c", *pAlias); /* "%c" displays whatever the parameter evaluates to as an ASCII character, hence dereference */
  23.     pAlias--;              /* Move back one character. */
  24.   }
  25.  
  26.   return 0;
  27. }
  28.